/*********************************************************************
 *    Project Name : tscp-platform
 *    Package Name : com.tscp.platform.web.util
 *    Create  Time : 2020/12/9 11:23
 *    =============================================================
 *    (C) Copyright Trustcro Information Technology Co., Ltd.
 *          Corporation 2018-2020 All Rights Reserved.
 *
 *	  【注意： 本内容仅限于长沙信立诚医药科技有限公司内部使用，禁止转发】
 **********************************************************************/
package com.trust.pams.util;

import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
//import org.apache.poi.xssf.usermodel.XSSFCell;
//import org.apache.poi.xssf.usermodel.XSSFRow;
//import org.apache.poi.xssf.usermodel.XSSFSheet;
//import org.apache.poi.xssf.usermodel.XSSFWorkbook;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.io.*;
import java.util.Objects;
import java.util.StringJoiner;

/**
 * @Project : tscp-platform
 * @Author : Chasonx<zc.x>
 * @CreateTime : 2020/12/9 11:23
 * @Desc :
 * @Version : 1.0
 */
public class MdUtil {

    private static final Logger log = LoggerFactory.getLogger(MdUtil.class);

    public static final String MedDRA_PATH = "medDRA";
    public static final String WHODrug_PATH = "whoDRUG";

    public static final String REGEX_PREFIXX = "^.*";
    public static final String REGEX_SUFFIX = ".*$";

    public interface Language {
        String Chinese = "Chinese";
        String English = "English";
    }

    public static final int MATCH = 1;
    public static final int UN_MATCH = 0;

    public static final String MedDra_File_Cache_Prefix = "MC-mdFileDataCacheKey-";
    public static final String MedDra_Cache_Prefix = "MC-medDataCacheKey-";
    public static final String WhoDrug_File_Cache_Prefix = "MC-wFiledDataCacheKey-";
    public static final String WhoDrug_Cache_Prefix = "MC-whoDataCacheKey-";


    public static final String[] MedDRA_Collections = {
            "md_version",
            "md_hlgt",
            "md_hlgt_hlt",
            "md_htl",
            "md_hlt_pt",
            "md_intl_ord",
            "md_llt",
            "md_mdhier",
            "md_pt",
            "md_smq_content",
            "md_smq_list",
            "md_soc",
            "md_soc_hlgt",
            "md_history_chinese"
    };


    public static String getClassName(String prefix,String fileName){
        return prefix + toUpperCaseFirstChar(convertCamelCaseName(getSimpleFileName(fileName)));
    }

    public static String convertCamelCaseName(String name){
        char[] chars = name.toCharArray();
        StringBuilder sb = new StringBuilder();
        boolean step = false;
        for(char c : chars){
            if(c == '_'){
                step = true;
                continue;
            }
            if(step) {
                step = false;
                sb.append(Character.toUpperCase(c));
            }else{
                sb.append(c);
            }
        }
        return sb.toString();
    }

    public static String toUpperCaseFirstChar(String str){
        return Character.toUpperCase(str.charAt(0)) + str.substring(1);
    }

    public static String getSimpleFileName(String fileName){
        return fileName.substring(0,fileName.lastIndexOf("."));
    }

    private static final String sheetKey = "sheetIdx";
    private static final String sheetName = "sheetName";
    private static final String rowDataKey = "rowData";
    private static final String colDataKey = "colData";
 /*   public static File gennerMedDraReport(JSONArray array, File file){
        try(InputStream is = new FileInputStream(file);
            XSSFWorkbook workbook = new XSSFWorkbook(is);
            FileOutputStream os = new FileOutputStream(file)) {

            JSONObject object;
            int sheetIdx = 0;
            String currentSheetName;
            XSSFSheet sheet;
            JSONArray rowData;
            int j = 0,jlen = 0;
            int c = 0,clen = 0;

            JSONObject rowObj;
            JSONArray listCol;
            String colName;
            XSSFCell cell;
            for(int i = 0,len = array.size();i < len;i++){
                object = array.getJSONObject(i);
                sheetIdx = object.getInteger(sheetKey);
                currentSheetName = object.getString(sheetName);
                sheet = workbook.getSheetAt(sheetIdx);
                rowData = object.getJSONArray(rowDataKey);
                listCol = object.getJSONArray(colDataKey);
                clen = listCol.size();

                XSSFRow colRow = sheet.createRow(0);
                for(c = 0;c < clen;c++){
                    cell = colRow.createCell(c);
                    cell.setCellValue(fmtColName(listCol.getString(c) ,currentSheetName ));
                }

                for(j = 0,jlen = rowData.size();j < jlen;j++){
                    rowObj = rowData.getJSONObject(j);

                    XSSFRow row = sheet.createRow(j + 1);
                    for(c = 0;c < clen;c++){
                        cell = row.createCell(c);
                        colName = listCol.getString(c);
                        cell.setCellValue(Objects.isNull(rowObj.getString(colName)) ? "" : rowObj.getString(colName));
                    }
                }
            }
            workbook.write(os);
            os.flush();
            return file;
        } catch (IOException e) {
            log.error("gennerMedDraReport exception => " , e);
        }
        return null;
    }*/

    public static String fmtRegexVal(String value){
        StringJoiner joiner = new StringJoiner(".*");
        for(char c : value.toCharArray()){
            joiner.add(c + "");
        }
        return joiner.toString();
    }

    private static String fmtColName(String colName,String sheetName){
        if(colName.startsWith("$$")){
            return colName.replace("$$" , sheetName).toUpperCase();
        }
        return colName.toUpperCase();
    }
}
